<?php

namespace App\Jobs\QiPao\Draw;

use App\Enums\AppJumpEnum;
use App\Exceptions\BasicException;
use App\Jobs\Job;
use App\Repositories\GiftRepository;
use App\Repositories\Pet\PetPropsRepository;
use App\Services\Draw\DrawConst\CacheKey;
use App\Services\Draw\Enum\DrawEnum;
use App\Services\Tools\PushService;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class StellarDrawStatisticsJob extends Job
{
    protected $statisticsData;
    protected $poolGroup;
    protected $user;
    protected $cachePrefix;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($poolGroup, $user, $statisticsData, $cachePrefix)
    {
        $this->statisticsData = $statisticsData;
        $this->poolGroup      = $poolGroup;
        $this->user           = $user;
        $this->cachePrefix    = $cachePrefix;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $date  = date('Ymd');
        $week  = date('YW');
        $month = date('Ym');
        $year  = date('Y');
        try {
            $redisObject = app('redis')->connection('game');
            $poolConfig  = app('model_stellar_pool_config')->info();
            //$this->staticsSeasonData($poolConfig, $redisObject);
            //数据统计
            $userId = $this->user->user_id;
            $redisObject->setbit($this->cachePrefix . CacheKey::USER_STATISTICS_COUNT_DATE . "_" . $date, $userId, 1);
            $redisObject->setbit($this->cachePrefix . CacheKey::USER_STATISTICS_COUNT_WEEK . "_" . $week, $userId, 1);
            $redisObject->setbit($this->cachePrefix . CacheKey::USER_STATISTICS_COUNT_MONTH . "_" . $month, $userId, 1);
            $redisObject->setbit($this->cachePrefix . CacheKey::USER_STATISTICS_COUNT_YEAR . "_" . $year, $userId, 1);


            //数据统计
            $dateStatistics             = app('model_stellar_statistics')->firstOrCreate(['time' => $date, 'type' => 1], ['time' => $date, 'type' => 1]);
            $weekStatistics             = app('model_stellar_statistics')->firstOrCreate(['time' => $week, 'type' => 2], ['time' => $date, 'type' => 1]);
            $monthStatistics            = app('model_stellar_statistics')->firstOrCreate(['time' => $month, 'type' => 3], ['time' => $date, 'type' => 1]);
            $yearStatistics             = app('model_stellar_statistics')->firstOrCreate(['time' => $year, 'type' => 4], ['time' => $date, 'type' => 1]);
            $dateStatistics->input      += $this->statisticsData['input'];
            $dateStatistics->output     += $this->statisticsData['output'];
            $dateStatistics->count      += $this->statisticsData['count'];
            $dateStatistics->user_count = $redisObject->bitcount($this->cachePrefix . CacheKey::USER_STATISTICS_COUNT_DATE . "_" . $date);
            $dateStatistics->save();

            $weekStatistics->input      += $this->statisticsData['input'];
            $weekStatistics->output     += $this->statisticsData['output'];
            $weekStatistics->count      += $this->statisticsData['count'];
            $weekStatistics->user_count = $redisObject->bitcount($this->cachePrefix . CacheKey::USER_STATISTICS_COUNT_WEEK . "_" . $week);

            $weekStatistics->save();

            $monthStatistics->input      += $this->statisticsData['input'];
            $monthStatistics->output     += $this->statisticsData['output'];
            $monthStatistics->count      += $this->statisticsData['count'];
            $monthStatistics->user_count = $redisObject->bitcount($this->cachePrefix . CacheKey::USER_STATISTICS_COUNT_MONTH . "_" . $month);
            $monthStatistics->save();

            $yearStatistics->input      += $this->statisticsData['input'];
            $yearStatistics->output     += $this->statisticsData['output'];
            $yearStatistics->count      += $this->statisticsData['count'];
            $yearStatistics->user_count = $redisObject->bitcount($this->cachePrefix . CacheKey::USER_STATISTICS_COUNT_YEAR . "_" . $year);
            $yearStatistics->save();

            //统计道具赛季数据

        } catch (\Exception $e) {
            Log::error('星际宝箱抽奖统计错误：' . $e->getMessage());
        }

    }
}
